const EalenToken = artifacts.require("EalenToken.sol");
const Exchange = artifacts.require("Exchange.sol");
const ETH_ADDRESS = "0x0000000000000000000000000000000000000000";

const fromWei = bn => {
    return web3.utils.fromWei(bn, "ether");
};

const toWei = number => {
    return web3.utils.toWei(number.toString(), "ether");
};

const wait = (seconds) => {
    const milliseconds = seconds * 1000;
    return new Promise(resolve => setTimeout(resolve, milliseconds));
}

module.exports = async function (callback) {
    try {
        const token = await EalenToken.deployed();
        const exchange = await Exchange.deployed();
        const accounts = await web3.eth.getAccounts();

        // 第一步account0 ---> account1 10w
        await token.transfer(accounts[1], toWei(100000), { from: accounts[0] });

        // 第二步account0 ---> 交易所存入 10以太坊
        await exchange.depositEther({ from: accounts[0], value: toWei(10) });

        console.log("account[0]-在交易所的以太坊", fromWei(await exchange.tokens(ETH_ADDRESS, accounts[0])));

        // 第二步account0 ---> 交易所存入 10w个代币
        await token.approve(exchange.address, toWei(100000), { from: accounts[0] });

        await exchange.depositToken(token.address, toWei(100000), { from: accounts[0] });

        console.log("account[0]-在交易所的代币", fromWei(await exchange.tokens(token.address, accounts[0])));

        // 第三步account1 ---> 交易所存入 50以太坊
        await exchange.depositEther({ from: accounts[1], value: toWei(50) });

        console.log("account[1]-在交易所的以太坊", fromWei(await exchange.tokens(ETH_ADDRESS, accounts[1])));

        // 第三步account1 ---> 交易所存入 50000个代币
        await token.approve(exchange.address, toWei(50000), { from: accounts[1] });

        await exchange.depositToken(token.address, toWei(50000), { from: accounts[1] });

        console.log("account[1]-在交易所的代币", fromWei(await exchange.tokens(token.address, accounts[1])));



        // ----------------------------使用makeOrder方式下单----------------------------
        let orderId = 0;
        let res;

        // 创建订单
        res = await exchange.makeOrder(token.address, toWei(1000), ETH_ADDRESS, toWei(0.1), { from: accounts[0] });
        orderId = res.logs[0].args.id;
        console.log("创建了一个订单");
        await wait(1)

        // 取消订单
        res = await exchange.makeOrder(token.address, toWei(2000), ETH_ADDRESS, toWei(0.2), { from: accounts[0] });
        orderId = res.logs[0].args.id;
        await exchange.cancelOrder(orderId, { from: accounts[0] });
        console.log("取消了一个订单");
        await wait(1)

        // 完成订单
        res = await exchange.makeOrder(token.address, toWei(3000), ETH_ADDRESS, toWei(0.3), { from: accounts[0] });
        orderId = res.logs[0].args.id;
        await exchange.fillOrder(orderId, { from: accounts[1] });
        console.log("完成了一个订单");

        console.log("account[0]-在交易所的代币", fromWei(await exchange.tokens(token.address, accounts[0])));
        console.log("account[0]-在交易所的以太坊", fromWei(await exchange.tokens(ETH_ADDRESS, accounts[0])));

        console.log("account[1]-在交易所的代币", fromWei(await exchange.tokens(token.address, accounts[1])));
        console.log("account[1]-在交易所的以太坊", fromWei(await exchange.tokens(ETH_ADDRESS, accounts[1])));

        console.log("account[2]-在交易所的代币", fromWei(await exchange.tokens(token.address, accounts[2])));
    } catch (error) {
        console.log(error);
    }
    callback();
}